/**
 * module_config.js
 * 项目模块统一配置入口
 */

// 尝试加载各模块
let projectStore, researchStore, resourcesStore, plmProjectStore;

try {
  projectStore = require('./projectStore.js');
} catch (error) {
  console.error('加载projectStore失败，使用空对象', error);
  projectStore = {};
}

try {
  researchStore = require('./research_store.js');
} catch (error) {
  console.error('加载researchStore失败，使用空对象', error);
  researchStore = {};
}

try {
  resourcesStore = require('./resourcesStore.js');
} catch (error) {
  console.error('加载resourcesStore失败，使用空对象', error);
  resourcesStore = {};
}

try {
  plmProjectStore = require('./plm_project_store.js');
} catch (error) {
  console.error('加载plmProjectStore失败，使用空对象', error);
  // 创建一个最小化的plmProjectStore实现
  plmProjectStore = {
    // 状态定义
    loading: false,
    error: null,
    projects: [],
    pagination: { page: 1, size: 20, total: 0, hasMore: true },
    filters: {
      status: 'all',
      keyword: '',
      cate: 0,
      devManager: 0,
      bizManager: 0
    },
    
    // 计算属性
    get filteredProjects() {
      return this.projects;
    },
    
    get projectCount() {
      return this.projects.length;
    },
    
    // 行为方法
    async getProjectList(params = {}, forceRefresh = false) {
      console.log('备用getProjectList方法被调用', params, forceRefresh);
      this.loading = true;
      
      // 模拟异步加载延迟
      await new Promise(resolve => setTimeout(resolve, 500));
      
      // 模拟项目数据
      const mockProjects = [
        { 
          _id: 'mock001', 
          model: '模拟项目1', 
          status: 1,
          CREATETIME: Math.floor(Date.now() / 1000) - 86400, 
          UPDATETIME: Math.floor(Date.now() / 1000)
        },
        { 
          _id: 'mock002', 
          model: '模拟项目2', 
          status: 2,
          CREATETIME: Math.floor(Date.now() / 1000) - 172800, 
          UPDATETIME: Math.floor(Date.now() / 1000)
        }
      ];
      
      this.projects = mockProjects;
      this.pagination = {
        page: params.page || 1,
        size: params.size || 20,
        total: mockProjects.length,
        hasMore: false
      };
      
      this.loading = false;
      
      return {
        list: mockProjects,
        total: mockProjects.length
      };
    },
    
    async loadMoreProjects() {
      console.log('备用loadMoreProjects方法被调用');
      if (!this.pagination.hasMore || this.loading) return;
      
      const nextPage = this.pagination.page + 1;
      return this.getProjectList({ page: nextPage });
    },
    
    setFilters(filters) {
      console.log('备用setFilters方法被调用', filters);
      this.filters = {
        ...this.filters,
        ...filters
      };
    },
    
    clearAllCache() {
      console.log('备用clearAllCache方法被调用');
      this.projects = [];
      this.pagination = { page: 1, size: 20, total: 0, hasMore: true };
      this.error = null;
    }
  };
}

// 导出模块
module.exports = {
  projectStore,
  researchStore,
  resourcesStore,
  plmProjectStore
}; 